var api = require('../../../utils/api.js')
var app = new getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: 0,
    isIphoneX: false,
    show:false,
    selectLocate:null,
    province:"省份",
    city:"城市",
    path:"https://siniso.fstuis.com",
    shopData:[],
    empty:false
  },
  showPopup(){
    this.setData({
      show:true
    })
  },
  close(e){
    this.setData({
      show:false,
      province:e.detail.province,
      city:e.detail.city,
      c_id:e.detail.c_id
    })
    this.getData(app.globalData.lat,app.globalData.lng,'',e.detail.c_id)
  },
  toSearch(e) {
    let val = e.detail.value
    this.getData(app.globalData.lat,app.globalData.lng,val)
  },
  toSelect(){
    wx.navigateTo({
      url: '/pages/index/shops/search/search',
    })
  },
  toTel(e){
    let index = e.currentTarget.dataset.index
    let tel = this.data.shopData[index].tel;
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  toLocate(e){
    let index = e.currentTarget.dataset.index
    let latitude = this.data.shopData[index].latitude;
    let longitude = this.data.shopData[index].longitude;
    wx.openLocation({
      latitude: Number(latitude),
      longitude: Number(longitude),
      address: "当前定位"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getData(latitude, longitude,search,id ) {
    let params={
      latitude,
      longitude,
      search,
      area_id:id
    }
    api.post('stores',params).then(res => {
      if (res.code == 200) {
        if(res.data.length == 0){
          this.setData({
            empty:true
          })
          return;
        }
        this.setData({
          shopData:res.data,
          empty:false
        })
      }
    })
  },
  onShow: function (options) {
    let selectLocate = app.globalData.selectLocate
    if(selectLocate){
      this.setData({
        selectLocate
      })
    }
    this.getLocation()
  },
  //校验位置权限是否打开
  checkLocation() {
    let _that = this;
    //选择位置，需要用户授权
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.showToast({ //这里提示失败原因
                title: '授权成功！',
                duration: 1500
              })
              _that.getLocation()
            },
            fail() {
              _that.showSettingToast('需要授权位置信息');
            }
          })
        } else {
          _that.getLocation()
        }
      }
    })
  },
  // 打开权限设置页提示框
  showSettingToast: function (e) {
    let _that = this;
    wx.showModal({
      title: '提示！',
      confirmText: '去设置',
      showCancel: true,
      content: e,
      success: function (res) {
        if (res.confirm) {
          wx.openSetting({
            success: (res) => {
              if (res.authSetting["scope.userLocation"]) {
                _that.getLocation()
              }
            },
            fail: (res) => {
              console.log("授权失败")
            }
          })
        }
      },
      fail: function (res) {

      }
    })
  },

  // 获取定位当前位置的经纬度
  getLocation: function () {
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        let latitude = res.latitude
        let longitude = res.longitude
        app.globalData.lat = res.latitude; //
        app.globalData.lng = res.longitude; //把onload定位时候的经纬度存到全局
        that.getCity(latitude, longitude)
      },
      fail: function (res) {
        api.alert('需要授权位置信息,请打开手机定位！');
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  getCity(latitude,longitude){
    let params={
      latitude,
      longitude
    }
    api.post('get_china_addr',params).then(res => {
      if (res.code == 200) {
        this.setData({
          province:res.data.province,
          city:res.data.city
        })
        this.getData(latitude,longitude,'',res.data.city_id)
      }
    })
  },
  // 获取当前地理位置
  // getLocal: function (latitude, longitude) {
  //   let that = this;
  //   qqmapsdk.reverseGeocoder({
  //     location: {
  //       latitude: latitude,
  //       longitude: longitude
  //     },
  //     success: function (res) {
  //       let province = res.result.ad_info.province
  //       let city = res.result.ad_info.city
  //       let district = res.result.ad_info.district;
  //       // 保存一下当前定位的位置留着后面重新定位的时候搜索附近地址用
  //       app.globalData.currentLocation = district;
  //       that.setData({
  //         province: province,
  //         city: city,
  //         latitude: latitude,
  //         longitude: longitude,
  //         district: district
  //       })

  //     },
  //     fail: function (res) {
  //       console.log(res);
  //     },
  //     complete: function (res) {
  //       // console.log(res);
  //     }
  //   });
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function (options) {
    let flag = options.detail
    let type = options.type
    if (type) {
      this.setData({
        type
      })
    }
    if (flag) {
      this.setData({
        flag
      })
    }
    this.getCountHeight()
  },
  getCountHeight(){
    let isIphoneX = app.globalData.isIphoneX
    if (isIphoneX) {
      this.setData({
        isIphoneX
      })
    }
    this.setData({
      navH: app.globalData.navHeight
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // imageUrl: '../../imgs/index/share.jpg',
      // title: app.globalData.shareTitle,
      // path: '/pages/index/index'
    }
  }
})